﻿using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using HotelManageSystem.DAL.IntializerDataBase;
using HotelManageSystem.DAL.Repositories;
using HotelManageSystem.Domain;
using System.Data.Entity;
//using HotelManageSystem.DAL.IntializerDataBase;
using Microsoft.Practices.Unity;
using HotelManageSystem.IoC;
using HotelManageSystem.Domain.Infrastructure;

namespace HotelManageSystem
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Room", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new { controller = @"[^/.]*" }
            );

        }

        protected void Application_Start()
        {
            Database.SetInitializer<HotelManageSystemDbContext>(new DataBaseIntializer());
           // Database.SetInitializer<HotelManageSystemDbContext>(new DropCreateDatabaseIfModelChanges<HotelManageSystemDbContext>());

            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            IUnityContainer unityContainer = GetUnityContainer();
            DependencyResolver.SetResolver(new UnityDependencyResolver(unityContainer));
        }

        private IUnityContainer GetUnityContainer()
        {
            IUnityContainer unityContainer = new UnityContainer()
            .RegisterType<IControllerActivator, CustomControllerActivator>()
            .RegisterInstance<MembershipProvider>(Membership.Provider)
            .RegisterType<IDatabaseFactory, DatabaseFactory>(new HttpContextLifetimeManager<IDatabaseFactory>())
            .RegisterType<IUnitOfWork, UnitOfWork>(new HttpContextLifetimeManager<IUnitOfWork>())
            .RegisterType<IControllerFactory, UnityControllerFactory>()
            .RegisterType<IEssentialInfoRepository, EssentialInfoRepository>()
            .RegisterType<IRoomRepository, RoomRepository>(new HttpContextLifetimeManager<IRoomRepository>())
            .RegisterType<IWarehouseRepository, WarehouseRepository>()

            .RegisterType<IRoomStateRepository, RoomStateRepository>(new HttpContextLifetimeManager<IRoomStateRepository>())
            .RegisterType<IGodownentryRepository, GodownentryRepository>()
            .RegisterType<IFloorRepository, FloorRepository>(new HttpContextLifetimeManager<IFloorRepository>())
            .RegisterType<IRoomTypeRepository, RoomTypeRepository>(new HttpContextLifetimeManager<IRoomTypeRepository>())

            .RegisterType<IDepartmentRepository, DepartmentRepository>()
            .RegisterType<IPostRepository, PostRepository>()
            .RegisterType<IAttendanceRepository, AttendanceRepository>()
            .RegisterType<IWageRepository, WageRepository>()
            .RegisterType<IInsureRepository, InsureRepository>()
            .RegisterType<IFinanceRepository, FinanceRepository>()
            .RegisterType<IEmployeeRepository, EmployeeRepository>()

            .RegisterType<IProductRepository, ProductRepository>()
            .RegisterType<IProductProviderRepository, ProductProviderRepository>()
            .RegisterType<IProductCategoryRepository, ProductCategoryRepository>()
            .RegisterType<IProductDetailInRepository, ProductDetailInRepository>()
            .RegisterType<IInventoryRepository, InventoryRepository>()
            .RegisterType<IDeliveryorderRepository, DeliveryorderRepository>()
            .RegisterType<IProductDetailOutRepository, ProductDetailOutRepository>()
            .RegisterType<IMinimumstockRepository, MinimumstockRepository>()

             .RegisterType<IHotelingInfomationRepository, HotelingInfomationRepository>(new HttpContextLifetimeManager<IHotelingInfomationRepository>())
            .RegisterType<IClientRepository, ClientRepository>(new HttpContextLifetimeManager<IClientRepository>())
            .RegisterType<IClientTypeRepository, ClientTypeRepository>(new HttpContextLifetimeManager<IClientTypeRepository>())
            .RegisterType<ICertificateTypeRepository, CertificateTypeRepository>(new HttpContextLifetimeManager<ICertificateTypeRepository>())
            .RegisterType<IProductCategoryRepository, ProductCategoryRepository>(new HttpContextLifetimeManager<IProductCategoryRepository>())
            .RegisterType<IClientConsumptionRepository, ClientConsumptionRepository>(new HttpContextLifetimeManager<IClientConsumptionRepository>())
            .RegisterType<IHotelingHistoryRepository, HotelingHistoryRepository>(new HttpContextLifetimeManager<IHotelingHistoryRepository>())
            ;

            return unityContainer;
        }

    }
}